<?php


namespace app\api\model;


use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;
use think\Model;

/**
 * 派给药店的单的表记录
 * Class PharmacyOrderModel
 * @package app\api\model
 */
class PharmacyOrderModel extends Model
{
    protected $table = 'pharmacy_order';

    /**
     * 给药店派单
     * @param $pharmacyId
     * @param $orderId
     * @return int|string
     */
    public function addPharmacyOrder($pharmacyId, $orderId)
    {
        $data = ['order_id' => $orderId, 'pharmacy_id' => $pharmacyId, 'status' => 1, 'created_at' => time()];

        $userId = Db::name($this->table)->insertGetId($data);

        return $userId;

    }


    /**
     * 更新药店的订单状态
     * @param $pharmacyId
     * @param $status
     */
    public function updatePharmacyOrderStatus($pharmacyId, $orderId, $updateData)
    {
        $result = 0;
        try {

            $result = Db::name($this->table)
                ->where(function ($sql) use ($pharmacyId) {
                    if ($pharmacyId != null) {
                        $sql->where('pharmacy_id', $pharmacyId);
                    }
                })
                ->where('order_id', $orderId)
                ->where('status', '<>', 4)
                ->update($updateData);
        } catch (Exception $e) {
            throwBusinessException("更新失败");
        }

        return 0 < $result;

    }

    /**
     * 根据状态说药店的订单
     * @param $pharmacyId
     * @param $status
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function fetchAcceptPharmacyOrderByStatus($pharmacyId, $status)
    {

        $result = Db::name($this->table)
            ->where('pharmacy_id', $pharmacyId)
            ->where('status', $status)
            ->field("order_id, status")
            ->select();

        return $result;

    }

    /**
     * 获取订单分配记录
     * @param $orderId
     * @return array|false|\PDOStatement|string|Model|null
     */
    public function getPharmacyOrderInfo($orderId)
    {
        $result = null;
        try {
            $result = Db::name($this->table)
                ->where('order_id', $orderId)
                ->where('status', 'in', ['1', '2'])
                ->field("order_id, pharmacy_id, status")
                ->find();
        } catch (Exception $e) {
            throwBusinessException("订单分配不存在");
        }

        return $result;
    }



    /**
     * 更新药店的订单状态
     * @param $pharmacyId
     * @param $status
     */
    public function updatePharmacyOrderStatusById($pharmacyId, $id, $updateData)
    {
        $result = 0;
        try {

            $result = Db::name($this->table)
                ->where(function ($sql) use ($pharmacyId) {
                    if ($pharmacyId != null) {
                        $sql->where('pharmacy_id', $pharmacyId);
                    }
                })
                ->where('order_id', $id)
                ->where('status', '<>', 4)
                ->update($updateData);
        } catch (Exception $e) {
            throwBusinessException("更新失败");
        }

        return 0 < $result;

    }

}